package com.kymatrix.kycarbon.xsmd.kbpm.server.controller;

import cn.hutool.core.lang.Dict;
import com.kymatrix.kycarbon.xsmd.common.business.common.PageResp;
import com.kymatrix.kycarbon.xsmd.kbpm.server.base.CommonResult;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.enums.err.FlowConst;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.service.ProcessDefineService;
import com.kymatrix.kycarbon.xsmd.kbpm.server.core.service.ProcessInstanceService;
import com.kymatrix.kycarbon.xsmd.kbpm.server.model.form.IdParam;
import com.kymatrix.kycarbon.xsmd.kbpm.server.model.form.IdsParam;
import com.kymatrix.kycarbon.xsmd.kbpm.server.model.form.UpAndDownParam;
import com.kymatrix.kycarbon.xsmd.kbpm.server.model.form.define.PageQueryProcessDefineReq;
import com.kymatrix.kycarbon.xsmd.kbpm.server.model.form.define.PageQueryProcessDefineResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
    * 流程定义 前端控制器
    * </p>
*
* @author mldong
* @since 2023-09-26
*/
@RestController
@Api(tags = "流程定义管理")
@RequiredArgsConstructor
public class ProcessDefineController {
    private final ProcessDefineService processDefineService;
    private final ProcessInstanceService processInstanceService;

    /**
     * 删除流程定义
     */
    @PostMapping("/wf/processDefine/remove")
    @ApiOperation(value = "删除流程定义")
    public CommonResult<?> remove(@RequestBody IdsParam param) {
        processDefineService.removeBatchByIds(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 查询单个流程定义
     */
    @PostMapping("/wf/processDefine/detail")
    @ApiOperation(value = "查询单个流程定义")
    public CommonResult<PageQueryProcessDefineResp> detail(@RequestBody IdParam param) {
        PageQueryProcessDefineResp processDefine = processDefineService.findById(param.getId());
        return CommonResult.data(processDefine);
    }
    /**
     *分页查询流程定义列表
     */
    @PostMapping("/wf/processDefine/page")
    @ApiOperation(value = "分页查询流程定义列表")
    public CommonResult<PageResp<PageQueryProcessDefineResp>> page(@RequestBody PageQueryProcessDefineReq param) {
        return CommonResult.data(PageResp.build(processDefineService.page(param)));
    }
    
    /**
     * 启用/禁用流程定义
     */
    @PostMapping("/wf/processDefine/upAndDown")
    @ApiOperation(value = "启用/禁用流程定义")
    public CommonResult<?> upAndDown(@RequestBody UpAndDownParam param) {
        processDefineService.upAndDown(param);
        return CommonResult.ok();
    }
    
    /**
     * 启动流程实例
     */
    @PostMapping("/wf/processDefine/startAndExecute")
    @ApiOperation(value = "启动流程实例")
    public CommonResult<?> startAndExecute(@RequestBody Dict args) {
        Long processDefineId = args.getLong(FlowConst.PROCESS_DEFINE_ID_KEY);
        args.remove(FlowConst.PROCESS_DEFINE_ID_KEY);
        processInstanceService.startAndExecute(processDefineId,args);
        return CommonResult.ok();
    }


}
